﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SplenetiX
{
    class Menu
    {
        MenuButton Play, Solo, Multi, Settings, Exit, MapEditor;
        List<MenuButton> listButton = new List<MenuButton>();
        List<MenuButton> listSubButtonPlay = new List<MenuButton>();
        Texture2D logo, lineG, lineD, ombreBG;
        Sprite flecheG, flecheD;
        Cursor cursorMenu;
        MouseState state;
        string clickedLink;
        bool ismouseoverFG, ismouseoverFD;
        public int indexButtons = 0;
        float timer, timer2;



        public Menu()
        {
            clickedLink = "None";
        }

        public int Index
        {
            get { return indexButtons; }
            set { this.indexButtons = value; }
        }

        public string ClickedLink
        {
            get { return clickedLink; }
            set { this.clickedLink = value; }
        }

        public void Reset()
        {
            indexButtons = 0;
            clickedLink = "None";
        }

        public void LoadMenu(ContentManager contentManager)
        {
            logo = contentManager.Load<Texture2D>("Menu_Interface//logo");
            lineG = contentManager.Load<Texture2D>("Menu_Interface//lineGMenu");
            lineD = contentManager.Load<Texture2D>("Menu_Interface//lineDMenu");
            ombreBG = contentManager.Load<Texture2D>("Menu_Interface//BG_supOmbre");


            flecheG = new Sprite(new Vector2(287, 210));
            flecheG.LoadContent(contentManager, "Menu_Interface//flecheGauche");
            flecheD = new Sprite(new Vector2(493, 210));
            flecheD.LoadContent(contentManager, "Menu_Interface//flecheDroite");

            Play = new MenuButton("Play", new Vector2(200, 200), 0);
            Settings = new MenuButton("Settings", new Vector2(500, 200), 1);
            MapEditor = new MenuButton("MapEditor", new Vector2(800, 200), 2);
            Exit = new MenuButton("Exit", new Vector2(-100, 200), 3);

            Solo = new MenuButton("Solo", new Vector2(220, 368), 4);
            Multi = new MenuButton("Multi", new Vector2(470, 368), 5);

            cursorMenu = new Cursor();
            cursorMenu.LoadCursor(contentManager);



            listButton.Add(Play);
            listButton.Add(Settings);
            listButton.Add(Exit);
            listButton.Add(MapEditor);

            listSubButtonPlay.Add(Solo);
            listSubButtonPlay.Add(Multi);



            foreach (MenuButton b in listButton)
            {
                b.Load(contentManager, "Menu_Interface//" + b.Name + "_button");
            }

            foreach (MenuButton b in listSubButtonPlay)
            {
                b.Load(contentManager, "Menu_Interface//" + b.Name + "_button");
            }
        }

        public void UpdateMenu(GameTime gameTime, ContentManager content, GraphicsDevice graphics, Personnage joueur, Level level)
        {
            joueur.Reset(level);


            bool mouseOverInAllMenu = false;
            clickedLink = "None";

            UpdateFleches(content, gameTime, (Index != 4 && Index != 5));

            DeplacementSelection(gameTime);

            foreach (MenuButton b in listButton)
            {
                b.UpdateButton(gameTime, (Index != 4 && Index != 5));

                if (b.isMouseOver || Index == b.Index)
                {
                    b.Load(content, "Menu_Interface//" + b.Name + "_button_over");
                    if (b.isMouseOver)
                        mouseOverInAllMenu = true;
                }
                else b.Load(content, "Menu_Interface//" + b.Name + "_button");

                if (b.isClicked || (Index == b.Index && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Enter)))
                    clickedLink = b.Name;
            }


            foreach (MenuButton b in listSubButtonPlay)
            {
                if (Play.Position.X == 200)
                {
                    b.UpdateSubButton();
                    if (b.isMouseOver || Index == b.Index)
                    {
                        b.Load(content, "Menu_Interface//" + b.Name + "_button_over");
                        if (b.isMouseOver)
                            mouseOverInAllMenu = true;

                    }
                    else b.Load(content, "Menu_Interface//" + b.Name + "_button");

                    if (b.isClicked || (Index == b.Index && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Enter)))
                        clickedLink = b.Name;
                }
            }



            cursorMenu.UpdateCursor(graphics, mouseOverInAllMenu);
        }

        public void DeplacementSelection(GameTime gameTime)
        {
            timer += (float)
                   gameTime.ElapsedGameTime.TotalSeconds;

            if (timer >= 0.4f)
            {
                if ((Index == 0 && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Down)) ||
                    (Index == 5 && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Left)))
                {
                    indexButtons = 4;
                    timer = 0f;
                }

                else if ((Index == 4 && (ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Right))))
                {
                    indexButtons = 5;
                    timer = 0f;
                }
                else if (Index != 4 && Index != 5 && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Right))
                {
                    indexButtons++;
                    indexButtons = Math.Abs(indexButtons % 4);
                    timer = 0f;
                }
                else if (Index != 4 && Index != 5 && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Left))
                {
                    indexButtons = indexButtons + 3;
                    indexButtons = Math.Abs(indexButtons % 4);
                    timer = 0f;
                }
                else if ((Index == 4 || Index == 5) && ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Up))
                {
                    indexButtons = 0;
                    timer = 0f;
                }
            }
        }

        public void UpdateFleches(ContentManager content, GameTime gameTime, bool conditionAffichage)
        {
            state = new MouseState();
            state = Mouse.GetState();

            timer += (float)
                    gameTime.ElapsedGameTime.TotalSeconds;


            if ((ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Left) && conditionAffichage) || (state.X >= flecheG.Position.X && state.X <= flecheG.Position.X + flecheG.Texture.Width
                && state.Y >= flecheG.Position.Y && state.Y <= flecheG.Position.Y + flecheG.Texture.Height))
                ismouseoverFG = true;
            else ismouseoverFG = false;

            if ((ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Right) && conditionAffichage) || (state.X >= flecheD.Position.X && state.X <= flecheD.Position.X + flecheD.Texture.Width
                && state.Y >= flecheD.Position.Y && state.Y <= flecheD.Position.Y + flecheG.Texture.Height))
                ismouseoverFD = true;
            else ismouseoverFD = false;

            timer2 += (float)
                    gameTime.ElapsedGameTime.TotalSeconds;

            if (timer2 >= 0.2f)
            {

                foreach (MenuButton b in listButton)
                {
                    if (state.X >= flecheG.Position.X && state.X <= flecheG.Position.X + flecheG.Texture.Width && state.Y >= flecheG.Position.Y &&
                                state.Y <= flecheG.Position.Y + flecheG.Texture.Height && state.LeftButton == ButtonState.Pressed)
                    {
                        if (Index == 4 || Index == 5)
                            indexButtons = 0;

                        b.UpdateButtonWithArrowsClick(gameTime, "gauche");
                        timer2 = 0f;
                        indexButtons = indexButtons + 3;
                        indexButtons = Math.Abs(indexButtons % 4);
                    }
                    if (state.X >= flecheD.Position.X && state.X <= flecheD.Position.X + flecheD.Texture.Width && state.Y >= flecheD.Position.Y &&
                                state.Y <= flecheD.Position.Y + flecheD.Texture.Height && state.LeftButton == ButtonState.Pressed)
                    {
                        if (Index == 4 || Index == 5)
                            indexButtons = 0;

                        b.UpdateButtonWithArrowsClick(gameTime, "droite");
                        timer2 = 0f;
                        indexButtons++;
                        indexButtons = Math.Abs(indexButtons % 4);
                    }
                }
            }



            if (ismouseoverFG)
                flecheG.LoadContent(content, "Menu_Interface//flecheGauche_over");
            else flecheG.LoadContent(content, "Menu_Interface//flecheGauche");
            if (ismouseoverFD)
                flecheD.LoadContent(content, "Menu_Interface//flecheDroite_over");
            else flecheD.LoadContent(content, "Menu_Interface//flecheDroite");


        }

        public void DrawMenu(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(logo, new Rectangle(200, 0, logo.Width, logo.Height), new Color(222, 222, 222));

            flecheG.Draw(spriteBatch);
            flecheD.Draw(spriteBatch);

            if (Play.Position.X == 200)
            {
                int count = 1;
                foreach (MenuButton b in listSubButtonPlay)
                {
                    if (count == 1)
                    {
                        spriteBatch.Draw(lineG, new Rectangle(275, 262, lineG.Width, lineG.Height), Color.White);
                        count++;
                    }
                    if (count == 2)
                        spriteBatch.Draw(lineD, new Rectangle(450, 262, lineD.Width, lineD.Height), Color.White);
                    b.Draw(spriteBatch, 2);
                }


            }

            foreach (MenuButton b in listButton)
            {
                b.Draw(spriteBatch, 1);
            }
            spriteBatch.Draw(ombreBG, new Rectangle(0, 0, ombreBG.Width, ombreBG.Height), Color.White);
            cursorMenu.DrawCursor(spriteBatch);

        }

    }


    class MenuButton
    {
        string NameButton;
        Vector2 Pos;
        bool isclicked;
        bool ismouseover = false;
        Texture2D texture_button;
        float timer;
        MouseState state;
        int index;

        // Constructeur
        public MenuButton(string name, Vector2 position, int indexButton)
        {
            NameButton = name;
            Pos = position;
            index = indexButton;
        }

        public bool isClicked
        {
            get { return isclicked; }
            set { this.isclicked = value; }
        }

        public int Index
        {
            get { return index; }
            set { this.index = value; }
        }

        public bool isMouseOver
        {
            get { return ismouseover; }
            set { this.ismouseover = value; }
        }

        public string Name
        {
            get { return NameButton; }
            set { this.NameButton = value; }
        }

        public Vector2 Position
        {
            get { return Pos; }
            set { this.Pos = value; }
        }

        public void Load(ContentManager contentManager, string assetName)
        {
            texture_button = contentManager.Load<Texture2D>(assetName);

        }

        public void UpdateButton(GameTime gameTime, bool conditionUpdate)
        {
            if (conditionUpdate)
            {

                timer += (float)
                gameTime.ElapsedGameTime.TotalSeconds;

                if (timer >= 0.2f)
                {
                    if (ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Left))
                    {
                        if (Pos.X < 800)
                            Pos.X += 300;
                        else Pos.X = -100;

                        timer = 0f;
                    }

                    if (ServiceHelper.Get<IKeyboardService>().IsKeyDown(Keys.Right))
                    {
                        if (Pos.X > -100)
                            Pos.X -= 300;
                        else Pos.X = 800;

                        timer = 0f;
                    }

                }

                state = new MouseState();
                state = Mouse.GetState();
                if (Position.X == 200)
                {
                    if (state.X >= Position.X + 121f && state.X <= Position.X + 297f && state.Y >= Position.Y + 7.5f && state.Y <= Position.Y + 62.5f)
                        ismouseover = true;
                    else ismouseover = false;

                    if (state.X >= Position.X + 121f && state.X <= Position.X + 297f && state.Y >= Position.Y + 7.5f &&
                        state.Y <= Position.Y + 62.5f && state.LeftButton == ButtonState.Pressed)
                    {
                        isclicked = true;
                    }
                    else isclicked = false;
                }
            }
        }

        public void UpdateButtonWithArrowsClick(GameTime gameTime, string typeFleche)
        {

            if (typeFleche == "gauche")
            {
                if (Pos.X < 800)
                    Pos.X += 300;
                else Pos.X = -100;
            }

            else if (typeFleche == "droite")
            {
                if (Pos.X > -100)
                    Pos.X -= 300;
                else Pos.X = 800;
            }

        }


        public void UpdateSubButton()
        {
            state = new MouseState();
            state = Mouse.GetState();


            if (state.X >= Position.X && state.X <= Position.X + 134f && state.Y >= Position.Y && state.Y <= Position.Y + 40f)
                ismouseover = true;
            else ismouseover = false;

            if (state.X >= Position.X && state.X <= Position.X + 134f && state.Y >= Position.Y &&
                    state.Y <= Position.Y + 40f && state.LeftButton == ButtonState.Pressed)
                isclicked = true;
            else isclicked = false;

        }

        public void Draw(SpriteBatch spriteBatch, int typeButton)
        {
            Color opaque = new Color(50, 50, 50);
            if (typeButton == 1)
            {
                if (Pos.X != 200)
                    spriteBatch.Draw(texture_button, Pos, opaque);

                else if (Pos.X == 200)
                    spriteBatch.Draw(texture_button, Pos, Color.White);
            }
            else if (typeButton == 2)
                spriteBatch.Draw(texture_button, Pos, Color.White);

        }
    }
}
